package com.jike.mahjong.service.Imp;

import com.jike.mahjong.common.Constants;
import com.jike.mahjong.common.Page;
import com.jike.mahjong.common.util.DateUtil;
import com.jike.mahjong.dao.StatisticsDao;
import com.jike.mahjong.model.CoinRecordModel;
import com.jike.mahjong.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by star on 2016/11/4.
 */
@Service("statisticsService")
public class StatisticsServiceImp implements StatisticsService {

    @Autowired
    private StatisticsDao statisticsDao;

    @Override
    public List<Map<String, Object>> queryIncomDiamondByDay(Map<String, Object> params) {
        return statisticsDao.queryIncomDiamondByDay(params);
    }

    @Override
    public List<Map<String, Object>> queryAllIncomDiamond() {
        return statisticsDao.queryAllIncomDiamond();
    }

    @Override
    public List<Map<String, Object>> queryPayDiamonByDay(Map<String, Object> params) {
    	params.put("tableName","jyzjh_game_log.DiamondLog"+new SimpleDateFormat("YYYYMM").format(new Date()));
        return statisticsDao.queryPayDiamonByDay(params);
    }

    @Override
    public List<Map<String, Object>> queryPayDiamonAll() {
    	Map<String, Object> para=new HashMap<>();
    	 para.put("tableName","jyzjh_game_log.DiamondLog"+new SimpleDateFormat("YYYYMM").format(new Date()));
        return statisticsDao.queryPayDiamonAll(para);
    }

    @Override
    public Page queryPayRecord(Map<String, Object> para, Page page) {
        int offset = (page.getCurrentPage() - 1) * page.getPageSize();
        para.put("offset", offset);
        para.put("pageSize", page.getPageSize());
        para.put("tableName","jyzjh_game_log.DiamondLog"+new SimpleDateFormat("YYYYMM").format(new Date()));
        List<CoinRecordModel> recordModelList = statisticsDao.queryPayRecord(para);
        int recordCount = statisticsDao.countPayRecord(para);
        page.setTotalCount(recordCount);
        page.setData(recordModelList);
        return page;
    }

    @Override
    public List<Map<String, Object>> queryShowPayDiamond(Map<String, Object> params) {
    	params.put("tableName","jyzjh_game_log.DiamondLog"+new SimpleDateFormat("YYYYMM").format(new Date()));
        return statisticsDao.queryShowPayDiamond(params);
    }

    @Override
    public List<Map<String, Object>> queryShowIncomeDiamond(Map<String, Object> params) {
        return statisticsDao.queryShowIncomeDiamond(params);
    }

    @Override
    public Page queryOrderInfoByAgentId(Map<String, Object> params, Page page) {
        int offset = (page.getCurrentPage() - 1) * page.getPageSize();
        params.put("offset", offset);
        params.put("pageSize", page.getPageSize());
        List<Map<String, Object>> recordModelList = statisticsDao.queryOrderInfoByAgentId(params);
        int recordCount = statisticsDao.countOrderInfoByAgentId(params);
        page.setTotalCount(recordCount);
        page.setData(recordModelList);
        return page;
    }

    @Override
    public Page queryRechargeInfoForAgent(Map<String, Object> params, Page page) {

        String sourceType = (String) params.get("sourceType");
        int offset = (page.getCurrentPage() - 1) * page.getPageSize();
        params.put("offset", offset);
        params.put("pageSize", page.getPageSize());
        List<Map<String, Object>> recordModelList = new ArrayList<Map<String, Object>>();
        int recordCount = 0;
        if (null == sourceType || Constants.ORDER_SOURCE_ALL.equals(sourceType)) {
            recordModelList = statisticsDao.queryRechargeInfoForAgent(params);
            recordCount = statisticsDao.countRechargeInfoForAgent(params);
        }

        if (Constants.ORDER_SOURCE_APP.equals(sourceType)) {
            recordModelList = statisticsDao.queryOrderInfoByAgentId(params);
            recordCount = statisticsDao.countOrderInfoByAgentId(params);
        }
        if (Constants.ORDER_SOURCE_PLATFORM.equals(sourceType)) {
            recordModelList = statisticsDao.queryRechargeRecordForAgent(params);
            recordCount = statisticsDao.countRechargeRecordForAgent(params);
        }
        page.setTotalCount(recordCount);
        page.setData(recordModelList);
        return page;
    }

    @Override
    public Map<String, Object> queryStatisticsInfo(Map<String, Object> params) {

        String currentTime = DateUtil.getCurrTimeByFomat();
        params.put("currentTime", currentTime);
        params.put("orderStatus", Constants.ORDER_STATUS_SUCCESS);

        float dayOrderIncome = 0;
        float dayPlatIncome = 0;
        float weekOrderIncome = 0;
        float weekPlatIncome = 0;
        float allOrderIncome = 0;
        float allPlatIncome = 0;
        if (statisticsDao.selectDayIncomeFromOrder(params) != null) {
            dayOrderIncome = statisticsDao.selectDayIncomeFromOrder(params)/100;
        }
        if (statisticsDao.selectDayIncomeFromPlat(params) != null) {
            dayPlatIncome=statisticsDao.selectDayIncomeFromPlat(params)/100;
        }
        if (statisticsDao.selectWeekIncomeFromOrder(params) != null) {
            weekOrderIncome = statisticsDao.selectWeekIncomeFromOrder(params)/100;
        }
        if (statisticsDao.selectWeekIncomeFromPlat(params) != null) {
            weekPlatIncome = statisticsDao.selectWeekIncomeFromPlat(params);
        }
        if (statisticsDao.selectAllIncomeFromOrder(params) != null) {
            allOrderIncome = statisticsDao.selectAllIncomeFromOrder(params)/100;
        }
        if (statisticsDao.selectAllIncomeFromPlat(params) != null) {
            allPlatIncome = statisticsDao.selectAllIncomeFromPlat(params)/100;
        }

        params.put("dayOrderIncome",dayOrderIncome );
        params.put("dayPlatIncome", dayPlatIncome);
        params.put("weekOrderIncome",weekOrderIncome );
        params.put("weekPlatIncome", weekPlatIncome);
        params.put("allOrderIncome",allOrderIncome );
        params.put("allPlatIncome", allPlatIncome);

        return params;
    }
}
